package com.java8.pangjunjie.stream;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Demo01 {
    public static void main(String[] args) {
        List<Integer> list1 = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        System.out.println(list1);
//        list1.add(1);
//        list1.add(3);
        List<Integer> list2 = new ArrayList<>();
        for(int n : list1){
            if(n%2==0){
                list2.add(n);
            }
        }
        System.out.println(list2);

        List<Integer> list3 = list1.stream().filter(new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) {
                return integer % 2 == 0;
            }
        }).collect(Collectors.toList());
        System.out.println(list3);

        List<Integer> list4 = list1.stream().filter(i->i%2==0).collect(Collectors.toList());
        System.out.println(list4);


        List<Integer> list5 = list1.stream().limit(3).collect(Collectors.toList());
        System.out.println(list5);

        List<Integer> list6 = list1.stream().sorted((n1,n2)->n2-n1).limit(3).collect(Collectors.toList());
        System.out.println(list6);

        List<Integer> list7 = list1.stream().map(item->item*5).collect(Collectors.toList());
        System.out.println(list7);

//        list1.stream().forEach(item-> System.out.println(item));
        list1.stream().forEach(System.out::println);



        List<Map<String,String>> mapList = new ArrayList<>();
        Map<String,String> map1 = new HashMap<>();
        map1.put("id","1");
        map1.put("name","管理员");
        mapList.add(map1);
        Map<String,String> map2 = new HashMap<>();
        map2.put("id","2");
        map2.put("name","运维");
        mapList.add(map2);

        System.out.println(mapList);

        List<Map<String,String>> mapList2 = mapList.stream().map(item->{
            Map<String,String> m = new HashMap<>();
            m.put("value",item.get("id"));
            m.put("text",item.get("name"));
            return m;
        }).collect(Collectors.toList());
        System.out.println(mapList2);


        List<Teacher> teacherList = Arrays.asList("张三","李四").stream().map(item->{
            return new Teacher(item);
        }).collect(Collectors.toList());
        System.out.println(teacherList);

        List<Teacher> tl = Arrays.asList("张三","李四2").stream().map(Teacher::new).collect(Collectors.toList());
        System.out.println(tl);


        IntSummaryStatistics statistics = list1.stream().mapToInt(n->n).summaryStatistics();

        System.out.println(statistics.getMax());
        System.out.println(statistics.getAverage());
        System.out.println(statistics.getCount());
        System.out.println(statistics.getMin());
    }
}
